#!/usr/bin/perl
use Getopt::Std;
getopt("d",\%args);
my $dir = $args{d};
#$dir ="/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_emerge/clean/beagle/run_beagle/beagle_mainrun";
#extract sample ids and sample id order
#$dir = $ARGV[0];
chomp($dir);
if($dir eq "")
{
        die "entered dir name where subfolders are created is empty\n";
}

system("gunzip $dir/1/1/beagle_out_1.geno_segment1.bgl.gz.phased.gz");
system("head -2 $dir/1/1/beagle_out_1.geno_segment1.bgl.gz.phased >$dir/temp_geno_segment_sampleids_phenotype");
open(WRBUFF,">$dir/23/beagle_out_female.geno_female_male.bgl.gz.phased");
open BUFF,"<$dir/temp_geno_segment_sampleids_phenotype" or die "Can't open $dir/temp_geno_segment_sampleids_phenotype : $!";
$line_sample = <BUFF>;
#print WRBUFF $line_sample;
@array_sampleid = split(/\s/,$line_sample);

shift(@array_sampleid);
shift(@array_sampleid);
#$line_pheno = <BUFF>;
print WRBUFF $line_pheno;
@array_pheno = split(/\s/,$line_pheno);
shift(@array_pheno);
shift(@array_pheno);
system("gunzip $dir/23/male/*.gz");
system("gunzip $dir/23/female/*.gz");
system("rm -rf $dir/temp_geno_segment_sampleids_phenotype");
#female
$ku =0;
open BUFF,"<$dir/23/female/beagleout_female.geno_female.bgl.gz.phased" or $ku =1;
open BUF,">>$dir/failed.txt";
if($ku ==1)
{
	print BUF "failed $dir/23/female/beagleout_female.geno_female.bgl.gz.phased\n";
}
else
{
#	print BUF "success\n";
}
$line_female_sample = <BUFF>;
@array_female_sampleid = split(/\s/,$line_female_sample);
shift(@array_female_sampleid);
shift(@array_female_sampleid);
#print @array_female_sampleid."\n";
$line_female_pheno = <BUFF>;
@array_female_pheno = split(/\s/,$line_female_pheno);
shift(@array_female_pheno);
shift(@array_female_pheno);
for($i=0;$i<@array_female_sampleid;$i++)
{
	$sample_pheno{$array_female_sampleid[$i]} = $array_female_pheno[$i];

}
#male
open MBUFF,"<$dir/23/male/beagleout_male.geno_male.bgl.gz.phased" or $ku =2;
if($ku ==2)
{
        print BUF "failed $dir/23/male/beagleout_male.geno_male.bgl.gz.phased\n";
}
else
{
	#print BUF "success\n";
}
$line_male_sample = <MBUFF>;
@array_male_sampleid = split(/\s/,$line_male_sample);
shift(@array_male_sampleid);
shift(@array_male_sampleid);
#print @array_male_sampleid."\n";
$line_male_pheno = <MBUFF>;
@array_male_pheno = split(/\s/,$line_male_pheno);
shift(@array_male_pheno);
shift(@array_male_pheno);
for($i=0;$i<@array_male_sampleid;$i++)
{
        $sample_pheno{$array_male_sampleid[$i]} = $array_male_pheno[$i];
}
$num_comb =  0;
for($i=0;$i<@array_female_sampleid;$i++)
{
	
	$combine_female_male_sample[$num_comb] = $array_female_sampleid[$i];
	$num_comb++;
}
for($i=0;$i<@array_male_sampleid;$i++)
{

        $combine_female_male_sample[$num_comb] = $array_male_sampleid[$i];
	$num_comb++;
	$combine_female_male_sample[$num_comb] = $array_male_sampleid[$i];
	$num_comb++;
}
#print @combine_female_male_sample."\n".@array_sampleid."\n";
for($i=0;$i<@array_sampleid;$i++)
{
	for($y=0;$y<@combine_female_male_sample;$y++)
	{
		if($array_sampleid[$i] eq $combine_female_male_sample[$y])
		{
		$newarray_order[$i] = $y;
		$newarray_order[$i+1] = $y+1;
		}
	$y++;
	}
	$i++;
}
#print "@newarray_order"."\n".@newarray_order."\n";
while($fline = <BUFF>)
{
	$mline = <MBUFF>;
	@female_snps = split(/\s/,$fline);
	@male_snps = split(/\s/,$mline);
	shift(@female_snps);
	shift(@male_snps);
	$rsid_f = shift(@female_snps);
	$rsid_m =  shift(@male_snps);
	if($rsid_f ne $rsid_m)
	{
		die "rsid of female not equal to rsid of male\n";
	}
	$line = "M $rsid_f";
	$num_comb =  0;
	for($i=0;$i<@female_snps;$i++)
	{
		$newarray_snps[$num_comb] = $female_snps[$i];
		$num_comb++;
	}
        for($i=0;$i<@male_snps;$i++)
        {
                $newarray_snps[$num_comb] = $male_snps[$i];
		$num_comb++;
		$newarray_snps[$num_comb] = $male_snps[$i];
		$num_comb++;
        }
	for($i=0;$i<@newarray_order;$i++)
	{
		$line = $line." ".$newarray_snps[$newarray_order[$i]]; 
	}
	print WRBUFF $line."\n";
}
